package com.cqut.ktpspring.controller;
import com.cqut.ktpspring.pojo.Course;
import com.cqut.ktpspring.pojo.Result;
import com.cqut.ktpspring.service.CourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@Slf4j
@RestController
public class CourseController {
    @Autowired
    private CourseService courseService;
    @PostMapping("/addCourse")
    public Result addCourse(@RequestBody Course course) {
        log.info("新增课程:{}",course);
        courseService.addCourse(course);
        return Result.success();
    }

    @GetMapping("/selectCourse")
    public Result selectCourse() {
        log.info("查询课程信息");
        List<Course> courselist =courseService.select();
        return Result.success(courselist);
    }

    @PutMapping("/updateCourse")
    public Result updateCourse(@RequestBody Course course) {
        log.info("更新课程信息：{}",course);
        courseService.update(course);
        return Result.success();
    }

    @DeleteMapping("/deleteCourse")
    public Result deleteCourse(@RequestParam String id) {
        log.info("根据id删除：{}",id);
        courseService.delete(id);
        return Result.success();
    }

    @GetMapping("/getCourseById")
    public Result getCourseByCno(@RequestParam String id) {
        log.info("根据课程id查询课程信息：{}",id);
        Course course=courseService.getCourseById(id);
        return Result.success(course);
    }
}
